import Vue from 'vue'
import VueRouter from 'vue-router'
import notFound from "@/views/404.vue"
import Main from "@/views/main.vue"
import { getStorageUserInfo } from '@/utils'
import { UserModule } from '@/store/modules/userModules'
Vue.use(VueRouter)

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location, onResolve, onReject) {
    if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
    return originalPush.call(this, location).catch(err => err)
}

export const privateRoutes = [
    {
        path: '/private',
        name: 'private',
        component: Main,
        meta: { permissions: [], title: '' },
        children: [

        ],
    },
]
export const publicRoutes = [
    {
        path: '/',
        name: 'home',
        component: Main,
        redirect: '/index',
        meta: { title: '首页' },
        children: [
            {
                path: '/index',
                name: 'index',
                component: () => import('@/views/index.vue'),
                meta: { title: '首页' },
            },
            {
                path: '/demand',
                name: 'demand',
                component: () => import('@/views/demand/demand.vue'),
                meta: { title: '创新需求' },
            },
            // 创新需求详情
            {
                path: '/demandDetail',
                name: 'demandDetail',
                component: () => import('@/views/demand/demandDetail.vue'),
                meta: { title: '创新需求' },
            },
            // 创新需求投递方案
            {
                path: '/demandDeliver',
                name: 'demandDeliver',
                component: () => import('@/views/demand/demandDeliver.vue'),
                meta: { title: '创新需求' },
            },

            {
                path: '/achievement',
                name: 'achievement',
                component: () => import('@/views/achievement/index.vue'),
                meta: { title: '成果直通车' },
            },
            // 成果直通车详情
            {
                path: '/achievementDetail',
                name: 'achievementDetail',
                component: () => import('@/views/achievement/detail.vue'),
                meta: { title: '成果详情' },
            },
            // 成果直通车对接
            {
                path: '/achievementDeliver',
                name: 'achievementDeliver',
                component: () => import('@/views/achievement/deliver.vue'),
                meta: { title: '成果对接' },
            },

            // 资源对接
            {
                path: '/resource',
                name: 'resource',
                component: () => import('@/views/resource/index.vue'),
                meta: { title: '资源对接' },
            },
            {
                path: '/resourceDetail',
                name: 'resourceDetail',
                component: () => import('@/views/resource/detail.vue'),
                meta: { title: '资源对接' },
            },
            // 成果交易
            {
                path: '/announcement',
                name: 'announcement',
                component: () => import('@/views/announcement/index.vue'),
                meta: { title: '成果交易' },
            },
            {
                path: '/announcementDetail',
                name: 'announcementDetail',
                component: () => import('@/views/announcement/detail.vue'),
                meta: { title: '成果交易' },
            },
            // 专家库
            {
                path: '/talent',
                name: 'talent',
                component: () => import('@/views/talent/index.vue'),
                meta: { title: '专家库' },
            },
            {
                path: '/talentDetail',
                name: 'talentDetail',
                component: () => import('@/views/talent/detail.vue'),
                meta: { title: '专家库' },
            },
            // 社区
            {
                path: '/community',
                name: 'community',
                component: () => import('@/views/community/index.vue'),
                meta: { title: '社区' },
            },
        ],
    },
]
const routes = [
    ...publicRoutes,
    {
        path: '/404',
        name: 'Page404',
        component: notFound
    },
    {
        path: '*',
        redirect: '/404'
    },
]
const router = new VueRouter({
    mode: 'hash',
    base: process.env.BASE_URL,
    routes: routes
})

router.beforeEach((to, from, next) => {
    UserModule.setNowRoute(to.path)
    document.getElementById('app').scrollTop = 0
    next()
    // let userInfo = getStorageUserInfo()

    // if (userInfo && userInfo.id) {
    //     //已登录
    //     next()
    // } else if (publicRoutesUrl.indexOf(to.path) !== -1) {
    //     //公开路由
    //     next()
    // } else {
    //     //
    //     next('/')
    // }

});

router.afterEach(route => {
    //
});
export const setRouter = (callback) => {
    const list = generateRoutes()
    for (let r of list) {
        router.addRoute(r)
    }
    callback()
}
const hasPermission = (permissions, route) => {
    if (route.meta && route.meta.permissions && route.meta.permissions.length > 0) {
        return permissions.some(permission => route.meta.permissions.includes(permission))
    } else {
        return true
    }
}
const filterAsyncRoutesWithPermissions = (routes, permissions) => {
    const res = []
    routes.forEach(route => {
        const r = { ...route }
        if (hasPermission(permissions, r)) {
            if (r.children) r.children = filterAsyncRoutesWithPermissions(r.children, permissions)
            res.push(r)
        }
    })
    return res
}
export const generateRoutes = () => {
    const userInfo = getStorageUserInfo()

    if (userInfo && userInfo.id) {
        const permissions =[]// userInfo.resourceChildren.map(item => item.name)
        return filterAsyncRoutesWithPermissions(privateRoutes, permissions)
    }
    return []
}
export default router
